/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/zephyr.h>
#include <drivers/gpio.h>

#define MASTER_STACK_SIZE 4096
/* spi 线程信息 */
struct k_thread gs_spi_master_thread_data;
K_THREAD_STACK_DEFINE(gs_spi_master_stack_area, MASTER_STACK_SIZE);
extern void spi_master_thread(void *v1, void *v2, void *v3);

/* 通过别名获取 "led0" 设备树 node id */
#define LED0_NODE DT_ALIAS(led0)
/* 通过 node id 获取 led 设备树信息 */
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

static void gpio_init(void)
{
    /* 检查硬件设备是否就绪 */
    int ret;
    if (!device_is_ready(led.port)) {
        return;
    }
    /* 将GPIO配置为输出并将其初始化为逻辑电平1 */
    ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
    if (ret < 0) {
        return;
    }
}

void main(void)
{
    gpio_init();

    /* 创建线程 */
    int pri = k_thread_priority_get(k_current_get());

    k_thread_create(&gs_spi_master_thread_data, gs_spi_master_stack_area,
                    K_THREAD_STACK_SIZEOF(gs_spi_master_stack_area), spi_master_thread, NULL,
                    NULL, NULL, pri, 0, K_NO_WAIT);

    while(1) {
        printk("Red say:Hello World! %s\n", CONFIG_BOARD);
        gpio_pin_toggle_dt(&led);
        k_msleep(1000);
    }
}
